//
//  WBUserInfo.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/27.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class WBUserInfo: NSObject,NSCoding {

    var access_token : String?
    
    var expires_in : TimeInterval = 0{
    
        didSet {
        
            expires_date = Date.init(timeIntervalSinceNow: expires_in)
        }
    
    }
    
    var uid : String?
    
    var screen_name : String?
    
    var profile_image_url : String?
    
    var expires_date: Date?
    
    init(dic:[String:Any]) {
        
        super.init()
        
        setValuesForKeys(dic)
    }
    
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    
    
    
    //归档
    func encode(with aCoder: NSCoder) {
        
        
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(profile_image_url, forKey: "profile_image_url")
        
        aCoder.encode(NSNumber.init(value: expires_in), forKey: "expires_in")
        aCoder.encode(expires_date, forKey: "expires_date")
    }
    
    //解档
     required init?(coder aDecoder: NSCoder) {
        
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        
        profile_image_url = aDecoder.decodeObject(forKey: "profile_image_url") as? String
        
        expires_in = aDecoder.decodeObject(forKey: "expires_in") as! TimeInterval
       
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        
        super.init()
    }
    
    
    func saveInfo() {
        
        NSKeyedArchiver.archiveRootObject(self, toFile: WBUserInfo.filePath())
        
    }
    
    
    
    class func readInfo()->WBUserInfo?{
    
        
       return NSKeyedUnarchiver.unarchiveObject(withFile: WBUserInfo.filePath()) as? WBUserInfo
    
    }
    
    
    private class func filePath() -> String{
    
        let path = NSString(string:"/Users/chenguanglei/Desktop")
        
        let file = path.appendingPathComponent("Plist.plist")
    
        return file
    
    }
    
    
}
